package com.sky.Screen;

import java.util.ArrayList;

import com.healthcare.widget.ActionBar;
import com.healthcare.widget.ActionBar.IntentAction;
import com.sky.Model.Item;
import com.sky.Model.LocationItem;
import com.sky.adapter.CustomGridViewAdapter;
import com.sky.heathcare.R;
import com.sky.heathcare.R.drawable;
import com.sky.heathcare.R.id;
import com.sky.heathcare.R.layout;
import com.sky.heathcare.R.menu;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

	 GridView gridView;
	 ArrayList<Item> gridArray = new ArrayList<Item>();
	  CustomGridViewAdapter customGridAdapter;
	  
	  boolean m_userStatus;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);       
        actionBar.setTitle("Home");

        
        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if(extras == null) {
            	m_userStatus = false;
            } else {
            	m_userStatus= extras.getBoolean("USER_STATUS");
            }
        } else {
        	m_userStatus= (Boolean) savedInstanceState.getSerializable("USER_STATUS");
        }
        
        initData();
        gridView = (GridView) findViewById(R.id.gridContent);
        customGridAdapter = new CustomGridViewAdapter(this, R.layout.row_grid, gridArray);
        gridView.setAdapter(customGridAdapter);
        
        
        gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				Item item = gridArray.get(position);
				gotoActivity(item);
				
			}
		});
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    private Intent getIntentFromString(String StringClassname)
    {
    	Class<?> c = null;
    	if(StringClassname != null) {
    	    try {
    	        c = Class.forName(StringClassname );
    	    } catch (ClassNotFoundException e) {
    	        // TODO Auto-generated catch block
    	        e.printStackTrace();
    	        return null;
    	    }
    	}
    	Intent intent = new Intent(this, c);
    	return intent;
    }
    
    public static Intent createIntent(Context context) {
        Intent i = new Intent(context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        return i;
    }
    
    private void gotoActivity(Item item)
    {
    	Intent intent = null;
    	if(item != null)
    	{
    		intent = getIntentFromString(item.getClassName());
    	}
    	    
    	if(intent != null)
    		startActivity(intent);
    }
    private void initData()
    {
    	Bitmap servicesIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.service);
        Bitmap schedule = BitmapFactory.decodeResource(this.getResources(), R.drawable.clendar);
        Bitmap qa = BitmapFactory.decodeResource(this.getResources(), R.drawable.qa);
        Bitmap knowlege = BitmapFactory.decodeResource(this.getResources(), R.drawable.knowlege);
        Bitmap trace = BitmapFactory.decodeResource(this.getResources(), R.drawable.trace);
        Bitmap born = BitmapFactory.decodeResource(this.getResources(), R.drawable.born);
        Bitmap setting = BitmapFactory.decodeResource(this.getResources(), R.drawable.setting);
        Bitmap home = BitmapFactory.decodeResource(this.getResources(), R.drawable.home);

        if(m_userStatus)
        {
        	gridArray.add(new Item(servicesIcon,"Dịch vụ","servicesActivity"));
            gridArray.add(new Item(schedule,"Đặt lịch","ScheduleActivity"));
            gridArray.add(new Item(qa,"Hỏi đáp","questionAnswerActivity"));
        }       
        gridArray.add(new Item(knowlege,"Kiến thức","KnowlegeActivity"));
        gridArray.add(new Item(trace,"Theo dõi","InspectionActivity"));
        gridArray.add(new Item(born,"Đặt nơi sinh","BornPlaceActivity"));
        gridArray.add(new Item(setting,"Cái đặt thông báo","NotificationActivity"));
        gridArray.add(new Item(home,"Thông tin","AboutActivity"));
        
    }
}
